package test001;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

public class JFontChooser extends JDialog {
    private JList<String> fontList;
    private JList<Integer> sizeList;
    private JCheckBox boldCheckBox;
    private JCheckBox italicCheckBox;
    private JLabel previewLabel;
    private Font selectedFont;
    private JButton okButton, cancelButton;

    public JFontChooser(Frame parent) {
        super(parent, "选择字体", true);
        setSize(500, 400);
        setLocationRelativeTo(parent);
        setLayout(new BorderLayout());

        initComponents();
        setupLists();
        setupListeners();
    }

    private void initComponents() {
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel listPanel = new JPanel(new GridLayout(1, 2));

        fontList = new JList<>();
        JScrollPane fontScrollPane = new JScrollPane(fontList);
        listPanel.add(fontScrollPane);

        Integer[] sizes = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
        sizeList = new JList<>(sizes);
        sizeList.setSelectedValue(12, true);
        JScrollPane sizeScrollPane = new JScrollPane(sizeList);
        listPanel.add(sizeScrollPane);

        mainPanel.add(listPanel, BorderLayout.CENTER);

        JPanel optionPanel = new JPanel();
        boldCheckBox = new JCheckBox("粗体");
        italicCheckBox = new JCheckBox("斜体");
        optionPanel.add(boldCheckBox);
        optionPanel.add(italicCheckBox);
        mainPanel.add(optionPanel, BorderLayout.NORTH);

        previewLabel = new JLabel("预览文本 - The quick brown fox jumps over the lazy dog");
        previewLabel.setHorizontalAlignment(JLabel.CENTER);
        previewLabel.setBorder(BorderFactory.createEtchedBorder());
        mainPanel.add(previewLabel, BorderLayout.SOUTH);

        add(mainPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        okButton = new JButton("确定");
        cancelButton = new JButton("取消");
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void setupLists() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = ge.getAvailableFontFamilyNames();
        List<String> filteredFonts = new ArrayList<>();
        for (String fontName : fontNames) {
            if (!fontName.contains("Hebrew") && !fontName.contains("Arabic") 
                && !fontName.contains("Thai") && !fontName.contains("Symbols")) {
                filteredFonts.add(fontName);
            }
        }
        fontList.setListData(filteredFonts.toArray(new String[0]));
        fontList.setSelectedValue("宋体", true);
    }

    private void setupListeners() {
        ListSelectionListener listListener = e -> updatePreview();
        ItemListener checkBoxListener = e -> updatePreview();

        fontList.addListSelectionListener(listListener);
        sizeList.addListSelectionListener(listListener);
        boldCheckBox.addItemListener(checkBoxListener);
        italicCheckBox.addItemListener(checkBoxListener);

        okButton.addActionListener(e -> {
            selectedFont = createSelectedFont();
            dispose();
        });

        cancelButton.addActionListener(e -> {
            selectedFont = null;
            dispose();
        });
    }

    private Font createSelectedFont() {
        String fontName = fontList.getSelectedValue();
        int size = sizeList.getSelectedValue();
        int style = Font.PLAIN;
        if (boldCheckBox.isSelected()) {
            style |= Font.BOLD;
        }
        if (italicCheckBox.isSelected()) {
            style |= Font.ITALIC;
        }
        return new Font(fontName, style, size);
    }

    private void updatePreview() {
        Font font = createSelectedFont();
        previewLabel.setFont(font);
    }

    public Font getSelectedFont() {
        return selectedFont;
    }

    public static Font showDialog(Component parent, String title, Font initialFont) {
        Frame owner = null;
        if (parent instanceof Frame) {
            owner = (Frame) parent;
        } else {
            owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
        }

        JFontChooser chooser = new JFontChooser(owner);
        if (title != null) {
            chooser.setTitle(title);
        }
        if (initialFont != null) {
            chooser.fontList.setSelectedValue(initialFont.getFamily(), true);
            chooser.sizeList.setSelectedValue(initialFont.getSize(), true);
            chooser.boldCheckBox.setSelected((initialFont.getStyle() & Font.BOLD) != 0);
            chooser.italicCheckBox.setSelected((initialFont.getStyle() & Font.ITALIC) != 0);
            chooser.updatePreview();
        }
        chooser.setVisible(true);
        return chooser.getSelectedFont();
    }
}    